package com.mmall.common;

import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import lombok.Getter;
import lombok.Setter;

import java.io.Serializable;

/**
 * @Author wangtengyu
 * @Create 2018-03-05-16:16
 * 高复用响应类
 */
@JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL)
@Getter
@Setter
public class ServerResponse<T> implements Serializable {

    private boolean status;
    private String msg;
    private T data;

    private ServerResponse(boolean status){
        this.status=status;
    }

    private ServerResponse(boolean status,String msg){
        this.status=status;
        this.msg=msg;
    }

    private ServerResponse(boolean status,T data){
        this.status=status;
        this.data=data;
    }

    private ServerResponse(boolean status,String msg,T data){
        this.status=status;
        this.msg=msg;
        this.data=data;
    }

    //正确的返回结果
    public static <T> ServerResponse<T> createBySuccess(){
        return new ServerResponse<T>(Const.ResponseStatus.SUCCESS);
    }

    public static <T> ServerResponse<T> createBySuccessMessage(String msg){
        return new ServerResponse<T>(Const.ResponseStatus.SUCCESS,msg);
    }

    public static <T> ServerResponse<T> createBySuccess(T data){
        return new ServerResponse<T>(Const.ResponseStatus.SUCCESS,data);
    }

    public static <T> ServerResponse<T> createBySuccess(String msg,T data){
        return new ServerResponse<T>(Const.ResponseStatus.SUCCESS,msg,data);
    }

    //错误的返回结果
    public static <T> ServerResponse<T> createByError(){
        return new ServerResponse<T>(Const.ResponseStatus.ERROR);
    }

    public static <T> ServerResponse<T> createByErrorMessage(String msg){
        return new ServerResponse<T>(Const.ResponseStatus.ERROR,msg);
    }






}
